﻿using System;
using System.Collections.Generic;

namespace SolidQBCPLibrary
{
    public class BCPObject
    {
        private string _tableName;
        private string _query;
        private List<string> _columnNames;

        public bool UseQueryAsSource;

        public bool IsDataSource;

        /// <summary>
        /// Optional, and only  available if the user specifies force_column_mappings
        /// </summary>
        public List<string> ColumnNames
        {
            get { return (_columnNames); }
        }

        public string TableName
        {
            get { return (_tableName); }
            set
            {
                if (String.IsNullOrEmpty(value))
                    throw new ArgumentException("TableName", "Not declared a valid TableName");

                _tableName = value;
            }
        }

        public string Query
        {
            get { return (_query); }
            set
            {
                if (String.IsNullOrEmpty(value))
                    throw new ArgumentException("Query", "Query cannot be empty");

                _query = value;
            }
        }



        public string Schema
        {
            get
            {
                string retorno = "";
                string[] s = this.TableName.Split('.');

                if (s.Length > 1)
                    retorno = s[0].Replace('[', ' ').Replace(']', ' ').Replace('"', ' ').Trim();
                else
                    retorno = String.Empty;

                return (retorno);
            }
        }

        public string TableWithoutSchema
        {
            get
            {
                string retorno = "";
                string[] s = this.TableName.Split('.');

                if (s.Length > 1)
                    retorno = s[1];
                else
                    retorno = s[0];

                retorno = retorno.Replace('[', ' ').Replace(']', ' ').Replace('"', ' ').Trim();

                return (retorno);
            }
        }

        public BCPObject() { }

        public BCPObject(string table_name,
                         bool useQueryAsSource, string query,
                         bool isDataSource)
        {

            TableName = table_name;

            UseQueryAsSource = useQueryAsSource;
            IsDataSource = isDataSource;

            if (useQueryAsSource && isDataSource)
                Query = query;
        }


        public void SetColumns(string commaSeparatedColumns)
        {
            _columnNames = new List<string>();
            foreach (string v in commaSeparatedColumns.ToString().Split(','))
            {
                _columnNames.Add(v.Trim());
            }
        }

    }
}
